<?php
/**
 * ============================================================================
 * Copyright (c) 2015-2018 贵州大师兄信息技术有限公司 All rights reserved.
 * siteַ: http://www.dsxcms.com
 * ============================================================================
 * @author:     David Song<songdewei@163.com>
 * @version:    v1.0.0
 * ---------------------------------------------
 * Date: 2018/9/14
 * Time: 上午9:17
 */

namespace App\Library\User;


use App\Library\AuthenticatedUser;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;

trait AddressManagers
{
    use AuthenticatedUser;

    /**
     * @return \Illuminate\Http\JsonResponse
     */
    public function save(Request $request)
    {

        $address_id = $request->input('address_id');
        $newaddress = Arr::only($request->input('address', []), [
            'consignee',
            'phone',
            'province',
            'city',
            'district',
            'street',
            'zipcode',
            'isdefault'
        ]);

        if (isset($newaddress['isdefault'])) {
            $this->user()->addresses()->update(['isdefault' => 0]);
        }

        if ($address_id) {
            $address = $this->user()->addresses()->find($address_id);
            if ($address) {
                $address->update($newaddress);
            }
        } else {
            $address = $this->user()->addresses()->create($newaddress);
        }
        return $this->sendAddressSavedResponse($request, $address);
    }

    /**
     * @param Request $request
     * @param $address
     * @return \Illuminate\Http\JsonResponse
     */
    protected function sendAddressSavedResponse(Request $request, $address)
    {
        return ajaxReturn(['address' => $address]);
    }

    /**
     * @return \Illuminate\Http\JsonResponse
     */
    public function setDefault(Request $request)
    {
        $address_id = $request->input('address_id', 0);
        $this->user()->addresses()->update(['isdefault' => 0]);
        $this->user()->addresses()->where('address_id', $address_id)->update(['isdefault' => 1]);
        return ajaxReturn();
    }

    /**
     * @return \Illuminate\Http\JsonResponse
     * @throws \Exception
     */
    public function delete(Request $request)
    {
        $address_id = $request->input('address_id', 0);
        $this->user()->addresses()->where('address_id', $address_id)->delete();
        return ajaxReturn();
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function get(Request $request)
    {
        $address_id = $request->get('address_id', 0);
        if ($address_id) {
            $address = $this->user()->addresses()->find($address_id);
        } else {
            $address = $this->user()->addresses()->orderByDesc('isdefault')->first();
        }

        return ajaxReturn(['address' => $address]);
    }

    /**
     * @return \Illuminate\Http\JsonResponse
     */
    public function batchget(Request $request)
    {
        $items = $this->user()->addresses()->orderBy('address_id')->get();
        return ajaxReturn(['items' => $items]);
    }
}
